package com.linxin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linxin.config.security.component.JwtTokenUtil;
import com.linxin.entity.Admin;
import com.linxin.entity.Role;
import com.linxin.mapper.AdminMapper;
import com.linxin.mapper.RoleMapper;
import com.linxin.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linxin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author linxin
 * @since 2022-05-04
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead; // token 头部信息
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result login(String username, String password) {
        // 登录
        UserDetails userDetails = userDetailsService.loadUserByUsername(username); // 调用权限框架方法获取用户名
        // 参数：第一个用户传过来的密码，第二个从 userDetails 中获取的
        if (null == userDetails || !passwordEncoder.matches(password, userDetails.getPassword())) {
            return Result.fail("用户名或密码不正确");
        }
        if (!userDetails.isEnabled()) {
            return Result.fail("账号被禁用，请联系管理员！");
        }
        // 自己补充其它判断：账号是否存在......

        // 更新 security 登录用户对象，设置到全局
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails
                , null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);

        // 登录成功，生成 token
        String token = jwtTokenUtil.generateToken(userDetails);
        System.out.println(token);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("tokenHead",tokenHead);
        tokenMap.put("token",token);
        return Result.succ(tokenMap);
    }
    //根据用户名获取用户信息
    @Override
    public Admin getAdminByUserName(String username) {
        return adminService.getOne(new QueryWrapper<Admin>().eq("username",username));
    }
    //根据 id 获取用户角色
    @Override
    public List<Role> getRoles(Integer id) {
        return roleMapper.getRoles(id);
    }


}
